package cn.tansanqinger.dao.sql.topic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.ITopicSqlDao;
import cn.tansanqinger.dao.sql.IUserSqlDao;
import cn.tansanqinger.pojo.Topic;
import cn.tansanqinger.pojo.User;

@Repository
public class TopicSqlDaoImpl {

	@Autowired
	private ITopicSqlDao dao;
	@Autowired
	private IUserSqlDao udao;
	
	/**
	 * 查询数据，通过条件查询
	 * @param grade
	 * @param keywords
	 * @param page
	 * @param limit
	 * @return
	 */
	public Map<String, Object> selectMessage(String keywords, int kispopup, int option, int page, int limit){
		QueryWrapper<Topic> queryWrapper = new QueryWrapper<Topic>();
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("tsn", keywords).or().like("ttitle", keywords));
		}
		if(kispopup!=-1) {
			queryWrapper.lambda().eq(Topic::getTispopup, kispopup);
		}
		if(option==1) {
			queryWrapper.eq("taudit", 1).eq("tisreport", 0).eq("tisquery", 0);
		} else if(option==2) {
			queryWrapper.eq("taudit", 0);
		} else if(option==3) {
			queryWrapper.eq("tisreport", 1);
		} else if (option==4) {
			queryWrapper.eq("tisquery", 1);
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("count", dao.selectCount(queryWrapper));
		Page<Topic> pg = new Page<Topic>(page, limit);
		IPage<Topic> iPage = dao.selectPage(pg, queryWrapper);
		List<Topic> coursesList = iPage.getRecords();
		for (int i = 0; i < coursesList.size(); i++) {
			if(coursesList.get(i).getTisidentity()==0) {
				coursesList.get(i).setTauname("管理员");
			} else {
				User user = udao.selectById(coursesList.get(i).getTauid());
				if(user!=null) {
					coursesList.get(i).setTauname("".equals(user.getName())?user.getPhone():user.getName());
				} else {
					coursesList.get(i).setTauname("未知");
				}
			}
		}
		resultMap.put("data", coursesList);
		resultMap.put("code","0");
		resultMap.put("msg", "查询成功");
		return resultMap;
	}
	
	
	
	/**
	 * 查询数据，查询全部数据
	 * @return
	 */
	public List<Topic> selectMessageAll(){
		return dao.selectList(null);
	}
	
	/**
	 * 获取编号
	 * @return
	 */
	public Integer serialNumber() {
		QueryWrapper<Topic> queryWrapper = new QueryWrapper<Topic>();
		queryWrapper.orderByDesc("tsn").last("limit 1");
		Topic object= dao.selectOne(queryWrapper);
		return object==null?10000001:object.getTsn()+1;
	}
	
	/**
	 * 添加信息
	 * @param course
	 * @return
	 */
	public int add(Topic object) {
		return dao.insert(object);
	}
	
	/**
	 * 查询信息
	 * @param cid
	 * @return
	 */
	public Topic selectOnt(int id) {
		return dao.selectById(id);
	}
	
	/**
	 * 修改状态
	 * @param cid
	 * @param isuse
	 * @return
	 */
	public int updateStatus(Topic object) {
		return dao.updateById(object);
	}
	
	/**
	 * 删除信息
	 * @param cid
	 * @return
	 */
	public int deleteOne(int id) {
		return dao.deleteById(id);
	}
	
	/**
	 * 修改信息
	 * @param user
	 * @return
	 */
	public int modification(Topic object) {
		return dao.updateById(object);
	}
}
